Vue.component('xcform_control_input_datetime', {
    template: '<el-form-item \
                  :class="value.fieldjsonObj.xcform_prop_required.value?\'required\':\'\'"\
                  :prop="value.fieldcode" \
                  :rules="control.rules"\
                  :label="value.fieldtitle"> \
                  <el-date-picker v-model="control.value" type="datetime" \
                      :format="value.fieldjsonObj.xcform_base_property_input_datetime.format" \
                      @change="handleDateChange" \
                      :placeholder="value.fieldjsonObj.xcform_base_placeholder.value" \
                      :disabled="control.disabled"></el-date-picker> \
                </el-form-item>',
    props: {
        value: {
            type: Object,
        },
        disabled: {
            type: Boolean,
        },
        fieldfulljson: {
            type: Array,
        },
        xcform_id: {
            type: Number,
        }
    },
    data() {
        var validateFunction = (rule, value, callback) => {
            var that = this;
            that.value.value = (that.value.value) ? that.value.value : "";

            if (that.value.fieldjsonObj.xcform_prop_required.value) {
                if (that.value.value.length == 0) {
                    return callback(new Error('不能为空'));
                }
            }
            return callback();
        };
        return {
            control: {
                value: null,
                disabled: false,
                rules: [{
                    validator: validateFunction,
                    trigger: 'blur'
                }]
            }
        };
    },
    created() {
        var that = this;
        that.control.disabled = that.disabled;
        if (that.value.fieldjsonObj.xcform_prop_readonly.value) {
            that.control.disabled = true;
        }

        that.control.value = that.timestampToDateTime(that.value.value);
    },
    mounted() {
        var that = this;

    },
    methods: {
        handleDateChange(value) {
            if (value) {
                this.value.value = value.getTime() / 1000; // 获取时间戳
            } else {
                this.value.value = null; // 如果未选择日期，则时间戳为 null
            }
        },
        timestampToDateTime(timestamp) {
            if(timestamp==null){
                return null;
            }
            const date = new Date(timestamp * 1000); // 时间戳需要乘以1000，因为JavaScript中的时间戳是以毫秒为单位的
            const year = date.getFullYear();
            const month = ('0' + (date.getMonth() + 1)).slice(-2); // 月份从0开始，所以需要+1
            const day = ('0' + date.getDate()).slice(-2);
            const hour = ('0' + date.getHours()).slice(-2);
            const minute = ('0' + date.getMinutes()).slice(-2);
    
            return `${year}-${month}-${day} ${hour}:${minute}`;
        }
    },
    
});